package com.coffeeloversupportlibrary.coffeelover.domain.dailyshare.share;

import android.util.Log;

import com.alibaba.sdk.android.oss.OSSClient;
import com.coffeeloversupportlibrary.coffeelover.callback.ModelCallBack;
import com.coffeeloversupportlibrary.core.base.BaseViewModel;
import com.coffeeloversupportlibrary.core.base.livadata.SingleLiveEvent;
import com.coffeeloversupportlibrary.core.data.DataRepository;
import com.huantansheng.easyphotos.models.album.entity.Photo;
import com.lzy.ninegrid.ImageInfo;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * @author puyv
 * @createTime 2021/11/24 9:29
 * @projectName CoffeeLoverSupportLibrary
 * @className ShareViewModel.java
 * @description TODO
 */
public class ShareViewModel extends BaseViewModel<ShareModel> {
    private SingleLiveEvent<List<ImageInfo>> selectPicture=new SingleLiveEvent<>();
    private SingleLiveEvent<String> dialogEvent=new SingleLiveEvent<>();


    public SingleLiveEvent<List<ImageInfo>> getSelectPicture() {
        return selectPicture;
    }
    public SingleLiveEvent<String> getDialogEvent(){
        return this.dialogEvent;
    }
    /**
    * @date 2021/11/26 15:27
    * @params []
    * @return void
    * @description TODO  暴露给 UI 的数据调用方法,UI观察该数据 对数据变换做感知
     */
    public void providerData(ArrayList<Photo> photos){
        selectPicture.setValue(transfer(photos));
    }

    /**
    * @date 2021/11/26 16:34
    * @params []
    * @return void
    * @description TODO 取消选择 置空 NineGridView
    */
    public void clearSelectPicture(){
        List<ImageInfo> list=new ArrayList<>();
        selectPicture.setValue(list);
    }
    //图片直传  aliyun
    public void uploadImage(String title, String content, OSSClient client){
        // TODO: 2021/11/27   上传分享内容
        List<String> images=new ArrayList<>();
        for(ImageInfo info:selectPicture.getValue()){
            images.add(info.getThumbnailUrl());
        }

        mModel.uploadImage(images, client, new ShareModel.ImageLoadCallback<List<String>,String>() {
            @Override
            public void loadSuccess(List<String> urls) {
               // 获得图片路径上传成功 做分享上传请求
//                https://mercuryimg.oss-cn-beijing.aliyuncs.com/shareImg/
                uploadShare(title,content,urls);
                //测试结果
                // TODO: 2021/11/27 2021-11-27 15:16:54.279 9035-9103/com.coffeeloversupportlibrary E/LOADURL: https://mercuryimg.oss-cn-beijing.aliyuncs.com/shareImg/IMG4409354532103612748.png
            }

            @Override
            public void loadFailure(String s) {
                  //失败   取消dialog
                dialogEvent.setValue("uploadFailure!");
            }
        });
    }
    // 图片上传成功后上传分享内容
    public void uploadShare(String title,String content,List<String> urls){
              mModel.uploadShare(title, content, urls, new ModelCallBack<String, String>() {
                  @Override
                  public void onSuccess(String s) {
                      // TODO: 2021/11/27  通知到UI  展示成功对话框
                      if(s.equals("success")){
                          dialogEvent.setValue("shareSuccess!");
                      }
                  }
                  @Override
                  public void Failure(String s) {
                      // TODO: 2021/11/28   取消对话框
                      dialogEvent.setValue("shareFailure!");
                  }
              });
    }


    @Override
    protected void setModel() {
       mModel=new ShareModel(DataRepository.getInstance());
    }
    /**
    * @date 2021/11/26 15:29
    * @params [resultPaths]
    * @return java.util.List<com.lzy.ninegrid.ImageInfo>
    * @description TODO  转换路径数组  --  NineGridView 适配的数据实体
    */
    private List<ImageInfo> transfer(ArrayList<Photo> photos){
        List<ImageInfo> imageInfo=new ArrayList<>();
        for(int i=0;i<photos.size();i++){
            ImageInfo info=new ImageInfo();
            info.setThumbnailUrl(photos.get(i).path);
            info.setBigImageUrl(photos.get(i).path);
            imageInfo.add(info);
        }
        return imageInfo;
    }
}
